package com.hao.hash;

import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * TODO
 *
 * @author 97472
 * @version 1.0
 * @date 2021/5/24 14:50
 */
public class EmpHashTable implements MyHashTable<Employee>{
    public static void main(String[] args) {
        Employee employee1 = new Employee("1","小明",null,null);
        Employee employee2 = new Employee("13","小明",null,null);
        Employee employee3 = new Employee("15","小明",null,null);
        Employee employee4 = new Employee("81","小明",null,null);
        Employee employee5 = new Employee("43","小明",null,null);
        EmpHashTable empHashTable = new EmpHashTable();
        empHashTable.add(employee1);
        empHashTable.add(employee2);
        empHashTable.add(employee3);
        empHashTable.add(employee4);
        empHashTable.add(employee5);
        System.out.println(empHashTable.get("1"));
        System.out.println(empHashTable.get("15"));
        empHashTable.del(employee3);
        System.out.println(empHashTable.get("15"));
    }
    private static int hash = 0;
    Deque<Employee>[] empLinkedListArr = new LinkedList[20];

    final static int hash(Employee emp){
        if (emp != null && emp.getId() != null){
            return Integer.valueOf(emp.getId()) % 20;
        }
        return hash;
    }
    @Override
    public void add(Employee employee){
        int hash = hash(employee);
        if (empLinkedListArr[hash] == null){
            empLinkedListArr[hash] = new LinkedList();
        }
        empLinkedListArr[hash].add(employee);
    }

    @Override
    public void del(Employee employee) {
        int hash = hash(employee);
        Deque<Employee> employeeDeque = empLinkedListArr[hash];
        if (employee != null && employee.getId() != null){
            if (employeeDeque !=null && !employeeDeque.isEmpty()){
                Iterator<Employee> iterator = employeeDeque.iterator();
                while (iterator.hasNext()){
                    Employee employee1 = iterator.next();
                    if (employee.getId().equals(employee1.getId())){
                        iterator.remove();
                    }
                }
            }
        }
    }

    @Override
    public void update(Employee employee) {
        int hash = hash(employee);
        Deque<Employee> employeeDeque = empLinkedListArr[hash];
        if (employee != null && employee.getId() != null){
            if (employeeDeque !=null && !employeeDeque.isEmpty()){

            }
        }
    }

    @Override
    public Employee get(String id) {
        Employee employee = new Employee();
        employee.setId(id);
        int hash = hash(employee);
        Deque<Employee> employeeDeque = empLinkedListArr[hash];
        if (employeeDeque == null || id == null){
            return null;
        }
        if (!employeeDeque.isEmpty()){
            Iterator<Employee> iterator = employeeDeque.iterator();
            while (iterator.hasNext()){
                employee = iterator.next();
                if (id.equals(employee.getId())){
                    return employee;
                }
            }
        }
        return null;
    }

}
